package com.example.demo.message.business;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.example.demo.constants.SubmissionSourceModuleConstant;
import com.example.demo.domain.entity.TrainCommitRecord;
import com.example.demo.domain.entity.VojSubmissions;
import com.example.demo.enums.TrainEvalStatusEnum;
import com.example.demo.service.TrainCommitRecordService;
import com.example.demo.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.MapMessage;

/**
 * @author liguang03
 * @date 2024/3/13
 */
@Slf4j
@Service
public class TrainDataProcess extends AbstractDataProcessBase{

    @Resource
    private TrainCommitRecordService trainCommitRecordService;

    private static final List<String> TRAIN_EVAL_DONE = Arrays.asList(ERROR_OCCURRED, ALL_TEST_POINTS_FINISHED);


    @Override
    public String getBusiness() {
        return SubmissionSourceModuleConstant.TRAIN;
    }

    @Override
    public void processData(VojSubmissions vojSubmissions, MapMessage mapMessage) {
        log.info("【培训】评测完毕接收消息体 voj:{}, msg:{}", JSONUtil.toJsonStr(vojSubmissions), JSONUtil.toJsonStr(mapMessage));
        String event = "";
        try {
            event = mapMessage.getString("event");
        } catch (JMSException e) {
            log.error("", e);
        }
        if (StringUtils.isBlank(event) || !TRAIN_EVAL_DONE.contains(event)){
            log.info("【培训】评测完毕接收消息体：{} 不处理", JSONUtil.toJsonStr(mapMessage));
            return;
        }

        Long submissionId = vojSubmissions.getSubmissionId();
        TrainCommitRecord record = trainCommitRecordService.getBySubmissionId(submissionId);
        //计算分数 todo
        record.setScore(vojSubmissions.getSubmissionJudgeScore());
        record.setEvalStatus(TrainEvalStatusEnum.EVAL_DONE.getKey());
        trainCommitRecordService.updateById(record);
    }
}
